from flask import Blueprint, g
from flask.json import jsonify

from models import User

user_bp = Blueprint('user_bp', __name__)


@user_bp.route('/add', methods=['post'])
def add():
    User(username=g.params.get('username')).to_new()
    return jsonify(code=0, msg='成功')


@user_bp.route('/edit', methods=['put'])
def edit():
    user = User.objects(id=g.params.get('user_id')).first()
    user.username = g.params.get('username')
    user.to_update()
    return jsonify(code=0, msg='成功')


@user_bp.route('/delete', methods=['delete'])
def delete():
    user = User.objects(id=g.params.get('user_id')).first()
    user.to_delete()
    return jsonify(code=0, msg='成功')
